<template>
  <div>
    <el-form @submit.native.prevent ref="editForm">
      <el-row :gutter="24" type="flex" class="row-bg">
        <el-col :span="6">
          <el-form-item>
            <el-input
              v-model="input"
              prefix-icon="el-icon-search"
              placeholder="图片名称/合作商名称/备注"
              @keyup.enter.native="search()"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item>
            <el-button type="primary" @click="search()">查询 </el-button>
            <el-button type="primary" @click="add()">新增 </el-button>
            <el-button type="primary" @click="enable()">启用 </el-button>
            <el-button type="primary" @click="disable()">禁用 </el-button>
            <el-button type="primary" @click="deleteSelection"
              >批量删除
            </el-button>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="8" :offset="6">
          <div class="grid-content">
            <el-form-item>
              <el-button type="primary" @click="enable()">启用 </el-button>
              <el-button type="primary" @click="disable()">禁用 </el-button>
              <el-button type="primary" @click="deleteSelection">删除
              </el-button>
            </el-form-item>
          </div>
        </el-col> -->
      </el-row>
    </el-form>

    <!--表格内容-->
    <el-table
      ref="list"
      :data="tableData"
      style="width: 100%; margin-bottom: 5px"
      border
      highlight-current-row
      :default-sort="{ prop: 'date', order: 'descending' }"
      @select-all="handleCheckedAllAndCheckedNone"
      @select="handleCheckedAllAndCheckedNone"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column type="selection"> </el-table-column>
      <el-table-column type="index" label="序号" width="50px">
      </el-table-column>
      <el-table-column property="partnername" label="合作商名称">
      </el-table-column>
      <el-table-column property="name" label="图片名称"> </el-table-column>
      <el-table-column property="url" label="LOGO">
        <template slot-scope="scope">
          <img
            :src="scope.row.url"
            alt="logo"
            style="width: 100%; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column property="remark" label="备注"> </el-table-column>
      <el-table-column property="jump" label="跳转路径">
        <template slot-scope="scope">
          <a
            :href="scope.row.jump"
            target="_blank"
            style="text-decoration: none"
            >{{ scope.row.jump }}</a
          >
        </template>
      </el-table-column>
      <el-table-column property="createTime" label="创建时间">
      </el-table-column>
      <el-table-column property="remark" label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="statusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150px">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="rowEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" size="mini" @click="rowDel(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      :page-sizes="[5, 10, 20, 50, 100]"
      :page-size="size"
      :current-page="current"
      :total="total"
      @current-change="currentChange"
      @size-change="sizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <!-- 弹出层 -->
    <!-- <LogoAdd ref="logoAdd" v-if="addShow" :addShow="addShow" @dialogClose="dialogClose"></LogoAdd> -->
    <!--<LogoEdit
ref="logoEdit"
v-if="editShow"
:editShow="editShow"
:record="formFileds"
@dialogClose="dialogClose"
@getList="getList"
    >
    </LogoEdit>-->

    <!-- 弹出层 -->
    <LogoAdd
      ref="logoAdd"
      v-if="addShow"
      :addShow="addShow"
      @dialogClose="dialogClose"
      @getList="getList"
    >
    </LogoAdd>
    <LogoEdit
      ref="logoEdit"
      v-if="editShow"
      :editShow="editShow"
      @dialogClose="dialogClose"
      :record="formFileds"
      @getList="getList"
    >
    </LogoEdit>
  </div>
</template>

<script>
import LogoAdd from "./logoAdd.vue";
import LogoEdit from "./logoEdit.vue";
import {
  plogo,
  search,
  delelte,
  deleltebatch,
  updatebatch,
  uplogo,
  ipage,
} from "@/api/systemManage/logoManage";
export default {
  name: "logoManage",
  components: {
    LogoAdd,
    LogoEdit,
  },
  data() {
    return {
      input: "",
      size: 10,
      current: 1,
      total: 0,
      formFileds: {},
      tableData: [],
      isShowAddDialog: false,
      isShowEditDialog: false,
      multipleSelection: null,
      addShow: false,
      editShow: false,
    };
  },
  methods: {
    search() {
      {
        this.getList();
        //  search("input=" + this.input).then((res) => {
        //});
      }
    },
    handleCheckedAllAndCheckedNone(selection) {
      // 当前选中仅一行时操作-（当前表格行高亮）
      1 != selection.length && this.$refs.list.setCurrentRow();
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    dialogClose() {
      this.addShow = false;
      this.editShow = false;
    },

    rowEdit(row) {
      //  console.log(1111)
      // this.$refs.detailRef.handleEdit('qqqq')

      // //当前选中行
      this.formFileds = row;
      this.editShow = true;
    },

    deleteSelection() {
      if (!this.multipleSelection) {
        this.$message.warning("请至少选择一条数据进行删除！");
        return;
      } else {
        this.$confirm("数据将会永久删除，是否继续？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }).then(() => {
          // console.log(this.multipleSelection);
          let array = [];
          for (let i = 0; i < this.multipleSelection.length; i++) {
            let obj = this.multipleSelection[i].id;
            array.push(obj);
          }
          let obj = { ids: array };
          deleltebatch(obj).then((res) => {
            this.getList(); //删除完成后初始化查询列表
          });
        });
      }
    },
    currentChange(e) {
      this.current = e;
      this.getList();
    },
    sizeChange(e) {
      this.size = e;
      this.getList();
    },
    rowDel(id) {
      this.$confirm("确定要删除当前行吗？", "删除", {
        comfirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        delelte("id=" + id).then((res) => {
          this.$message.success("删除成功!");
          this.getList();
        });
      });
    },
    enable() {
      if (this.multipleSelection == "") {
        this.$message.warning("请至少选择一条数据进行编辑！");
      } else {
        let array = [];
        let flag = "on";
        for (let i = 0; i < this.multipleSelection.length; i++) {
          let obj = this.multipleSelection[i].id;
          array.push(obj);
        }
        let obj = { ids: array, flag: flag };
        updatebatch(obj).then((res) => {
          this.getList();
        });
      }
    },
    disable() {
      if (this.multipleSelection == "") {
        this.$message.warning("请至少选择一条数据进行编辑！");
      } else {
        let array = [];
        let flag = "off";
        for (let i = 0; i < this.multipleSelection.length; i++) {
          let obj = this.multipleSelection[i].id;
          array.push(obj);
        }
        let obj = { ids: array, flag: flag };
        updatebatch(obj).then((res) => {
          this.getList();
        });
      }
    },
    statusChange(row) {
      let obj = Object.assign({}, row);
      if (obj.status == true) {
        obj.status = 0;
      } else {
        obj.status = 1;
      }
      obj.id = row.id;
      this.$delete(obj, "createTime");
      uplogo(obj).then((res) => {
        this.getList();
      });
    },
    add() {
      this.addShow = true;
    },
    getList() {
      var par;
      par = {
        input: this.input,
        size: this.size,
        current: this.current,
      };
      ipage(par).then((res) => {
        if (res) {
          this.tableData = res.data.records;
          this.total = res.data.total;
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].status == 0) {
              this.tableData[i].status = true;
            } else {
              this.tableData[i].status = false;
            }
          }
          for (let i = 0; i < this.tableData.length; i++) {
            let newdate = this.tableData[i].createTime;
            this.tableData[i].createTime = newdate.substring(0, 10);
          }
        }

        /* this.tableData = res.data; */
      });
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style scoped lang="less">
.el-form {
  padding: 0 10px;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

// .grid-content {
//   border-radius: 4px;
//   min-height: 36px;
// }

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
